package com.example.department.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.*;
import org.springframework.core.annotation.*;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Configuration
@EnableOpenApi
@RequiredArgsConstructor
public class SwaggerConfiguration {

    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Bean
    @Order(value = 1)
    public Docket docDocket() {
        return new Docket(DocumentationType.OAS_30).
                pathMapping("/doc")
                .enable(true)
                .apiInfo(groupApiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions("部门微服务"))
                ;
    }

    private ApiInfo groupApiInfo() {
        return new ApiInfoBuilder()
                .title("Knife4j接口文档")
                .description("Knife4j接口文档")
                .termsOfServiceUrl("https://doc.xiaominfo.com/")
                .version("1.0.0")
                .build();
    }

    /**
     * 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题

     @Bean public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
     ServletEndpointsSupplier servletEndpointsSupplier,
     ControllerEndpointsSupplier controllerEndpointsSupplier,
     EndpointMediaTypes endpointMediaTypes,
     CorsEndpointProperties corsProperties,
     WebEndpointProperties webEndpointProperties,
     Environment environment) {
     List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
     Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
     allEndpoints.addAll(webEndpoints);
     allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
     allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
     String basePath = webEndpointProperties.getBasePath();
     EndpointMapping endpointMapping = new EndpointMapping(basePath);
     boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
     return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
     }
     private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
     return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
     }
     **/
}
